﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler
{
    public class RemoveRoleCommendHandlers : IRequestHandler<RemoveRoleCommends, APIResult<RoleUser>>
    {
        private readonly IBaseRepository<RoleUser> ruRepository;

        public RemoveRoleCommendHandlers(IBaseRepository<RoleUser> ruRepository)
        {
            this.ruRepository = ruRepository;
        }

        /// <summary>
        /// 依据角色删除选择的用户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<RoleUser>> Handle(RemoveRoleCommends request, CancellationToken cancellationToken)
        {
            APIResult<RoleUser> result= new APIResult<RoleUser>();
            var rus = ruRepository.GetAll().Where(p => p.RoleId == request.RoleId).ToList();
            var data=ruRepository.DelList(rus).Result;
            if (data>0) {
                result.Code = APIEnum.删除成功;
                result.Msg=APIEnum.删除失败.ToString();

            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();

            }
            return Task.FromResult(result);
        }
    }
}
